/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.accessor.base.AccessorComparer;
import com.inspur.edp.cef.spi.entity.IAuthFieldValue;
import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class AssoEqualsMethodGenerator extends ClassMethodGenerator {

  @Override
  protected Block buildMethodBody() {
    Block block=ast.newBlock();
    IfStatement ifStatement = ast.newIfStatement();
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setLeftOperand(ast.newSimpleName("obj"));
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    infixExpression.setRightOperand(ast.newNullLiteral());
    ifStatement.setExpression(infixExpression);
    ReturnStatement returnFalseStatement = ast.newReturnStatement();
    returnFalseStatement.setExpression(ast.newBooleanLiteral(false));
    ifStatement.setThenStatement(returnFalseStatement);
    block.statements().add(ifStatement);

    ReturnStatement returnStatement=ast.newReturnStatement();
    MethodInvocation methodInvocation=ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName("equals"));
    methodInvocation.setExpression(ast.newName(AccessorComparer.class.getTypeName()));

    MethodInvocation methodInvocationArg1=ast.newMethodInvocation();
    methodInvocationArg1.setName(ast.newSimpleName("getValue"));

    methodInvocation.arguments().add(methodInvocationArg1);

    MethodInvocation methodInvocationArg2=ast.newMethodInvocation();
    methodInvocationArg2.setName(ast.newSimpleName("getValue"));
    CastExpression castExpression=ast.newCastExpression();
    castExpression.setType(ast.newSimpleType(ast.newSimpleName(IAuthFieldValue.class.getSimpleName())));
    addImport(IAuthFieldValue.class.getTypeName());
    castExpression.setExpression(ast.newSimpleName("obj"));
    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
    parenthesizedExpression.setExpression(castExpression);
    methodInvocationArg2.setExpression(parenthesizedExpression);
    methodInvocation.arguments().add(methodInvocationArg2);

    returnStatement.setExpression(methodInvocation);
    block.statements().add(returnStatement);
    return block;
  }

  protected boolean getIsOverride() {
    return true;
  }

  protected String getMethodName() {
    return "equals";
  }

  protected TypeInfo getReturnType() {
    return new TypeInfo("boolean");
  }

  protected ArrayList<ModifierKeyword> getAccessModifiers() {
    return new ArrayList(Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  protected ArrayList<ParameterInfo> getParameterCollection() {
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamName("obj");
    parameterInfo.setParamType(new TypeInfo(Object.class));
    return new ArrayList(Arrays.asList(parameterInfo));
  }
}
